package com.nd.android.musicboxlibrary.ui.fragment;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.common.utils.StarAppUtils;
import com.google.gson.Gson;
import com.nd.android.musicboxlibrary.GlobalSetting;
import com.nd.android.musicboxlibrary.R;
import com.nd.android.musicboxlibrary.business.bean.MusicInfo;
import com.nd.android.musicboxlibrary.business.comm.NdException;
import com.nd.android.musicboxlibrary.interf.ManagerFactory;
import com.nd.android.musicboxlibrary.ui.common.UnMixable;
import com.nd.android.musicboxlibrary.ui.common.ViewInjectorWithReflect;
import com.nd.android.musicboxlibrary.ui.mymusic.downloadmusic.MyMusicDownloadActivity;
import com.nd.android.musicboxlibrary.ui.mymusic.ilike.MyMusicLikeActivity;
import com.nd.android.musicboxlibrary.ui.mymusic.recentplay.MyMusicRecordActivity;
import com.nd.android.musicboxlibrary.ui.player.PlayerActivity;
import com.nd.android.musicboxlibrary.util.NdTask;
import com.product.android.utils.LogUtils;

/**
 * 
 * <br>
 * Created 2014年7月5日 下午4:11:54
 * 
 * @version
 * @author wat.ztag
 * 
 * @see
 */
public class MyMusicFragment extends Fragment implements OnClickListener {

	private ViewHolder holder = new ViewHolder();

	/**
	 * 
	 * <br>
	 * Created 2014年9月11日 下午11:40:15
	 * 
	 * @version MyMusicFragment
	 * @author wat.ztag
	 * 
	 * @see
	 */
	private class ViewHolder implements UnMixable {
		LinearLayout pnlIlike;
		LinearLayout pnlDownload;
		LinearLayout pnlPlayRecord;
		TextView txtPlayRecord, txtFav, txtDownload;
		TextView txtTitle;
        //我喜欢，下载音乐，最近播放
        TextView tv_i_like, tv_download_music, tv_recently_palyed;
		ImageView btnLeft, btnRight;
	}

	private List<MusicInfo> favList = new ArrayList<MusicInfo>();

	private List<MusicInfo> downLoadList = new ArrayList<MusicInfo>();

	private List<MusicInfo> lastPlayedList = new ArrayList<MusicInfo>();

	private Drawable leftDone, leftUndone;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		return inflater.inflate(R.layout.mbx_my_music_activity, container, false);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		ViewInjectorWithReflect.injectView(holder, this);
		initData();
		setupView();
		addListeners();
	}

	@Override
	public void onResume() {
		super.onResume();
		new GetMusicListOfFav(getActivity(), 0).executeByVersion();
		new GetMusicListOfDownLoad(getActivity(), 0).executeByVersion();
		new GetMusicListOfLastPlayed(getActivity(), R.string.mbx_runing).executeByVersion();
	}

	/**
	 * 
	 * <br>
	 * Created 2014年9月11日 下午11:40:18
	 * 
	 * @author wat.ztag
	 */
	protected void initData() {
		holder.txtTitle.setText(R.string.mbx_mymusic_title);
		leftDone = initDrawable(R.drawable.mbx_mymusic_finish);
		leftUndone = initDrawable(R.drawable.mbx_mymusic_loading);
	}

	/**
	 * 
	 * <br>
	 * Created 2014年9月11日 下午11:40:21
	 * 
	 * @author wat.ztag
	 */
	private void setupView() {
		holder.btnLeft.setVisibility(View.INVISIBLE);
		holder.btnRight.setVisibility(View.VISIBLE);
        StarAppUtils.setViewTypeFace(holder.txtDownload, false);
        StarAppUtils.setViewTypeFace(holder.txtFav, false);
        StarAppUtils.setViewTypeFace(holder.txtPlayRecord, false);
        StarAppUtils.setViewTypeFace(holder.txtTitle, false);
        //字体
        StarAppUtils.setViewTypeFace(holder.tv_i_like, false);
        StarAppUtils.setViewTypeFace(holder.tv_download_music, false);
        StarAppUtils.setViewTypeFace(holder.tv_recently_palyed, false);
	}

	/**
	 * 
	 * <br>
	 * Created 2014年9月11日 下午11:40:27
	 * 
	 * @author wat.ztag
	 */
	private void addListeners() {
		holder.btnRight.setOnClickListener(this);
		holder.pnlIlike.setOnClickListener(this);
		holder.pnlDownload.setOnClickListener(this);
		holder.pnlPlayRecord.setOnClickListener(this);
	}

	/**
	 * 
	 * <br>
	 * Created 2014年9月11日 下午11:40:47
	 * 
	 * @param drawableId
	 * @return Drawable
	 * @author wat.ztag
	 */
	private Drawable initDrawable(int drawableId) {
		Drawable drawable = getResources().getDrawable(drawableId);
		drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
		return drawable;
	}

	/**
	 * 
	 * <br>
	 * Created 2014年9月11日 下午11:40:51
	 * 
	 * @version MyMusicFragment
	 * @author wat.ztag
	 * 
	 * @see
	 */
	private class GetMusicListOfFav extends NdTask {

		/**
		 * Creates a new instance of GetMusicListOfFav. <br>
		 * Created 2014年9月11日 下午11:41:01
		 * 
         * @param context Context
         * @param resId int
		 */
		public GetMusicListOfFav(Context context, int resId) {
			super(context, resId);
		}

		@Override
		protected void doSuccess(Object result) {
			if (result != null) {
				favList = (List<MusicInfo>) result;
				int size = favList.size();
				int finishSize = 0;
				for (MusicInfo info : favList) {
					if (info.isDownLoaded()) {
						finishSize++;
					}
				}
				String strFinish = "";
				if (finishSize >= size) {
					strFinish = "全部";
					holder.txtFav.setCompoundDrawables(leftDone, null, null, null);
				} else {
					strFinish = finishSize + "首";
					holder.txtFav.setCompoundDrawables(leftUndone, null, null, null);
				}
				holder.txtFav.setText(getString(R.string.mbx_mymusic_fav_info, size, strFinish));
			}
		}

		@Override
		protected void doFail(NdException result) {
			Toast.makeText(mContext, result.getSimpleMsg(), Toast.LENGTH_LONG).show();
		}

		@Override
		protected Object doInBackground(Object... arg0) {
			return ManagerFactory.getPlaylistInstance(getActivity()).getMusicListOfFav();
		}
	}

	/**
	 * 
	 * <br>
	 * Created 2014年9月11日 下午11:41:06
	 * 
	 * @version MyMusicFragment
	 * @author wat.ztag
	 * 
	 * @see
	 */
	private class GetMusicListOfDownLoad extends NdTask {

		/**
		 * Creates a new instance of GetMusicListOfDownLoad. <br>
		 * Created 2014年9月11日 下午11:41:10
		 * 
         * @param context Context
         * @param resId int
		 */
		public GetMusicListOfDownLoad(Context context, int resId) {
			super(context, resId);
		}

		@Override
		protected void doSuccess(Object result) {
			if (result != null) {
				downLoadList = (List<MusicInfo>) result;
				holder.txtDownload.setText(getString(R.string.mbx_mymusic_music_info,
						downLoadList.size()));
			}
		}

		@Override
		protected void doFail(NdException result) {
			Toast.makeText(mContext, result.getSimpleMsg(), Toast.LENGTH_LONG).show();
		}

		@Override
		protected Object doInBackground(Object... arg0) {
			return ManagerFactory.getPlaylistInstance(getActivity()).getMusicListOfDownLoad();
		}
	}

	/**
	 * 
	 * <br>
	 * Created 2014年9月11日 下午11:41:14
	 * 
	 * @version MyMusicFragment
	 * @author wat.ztag
	 * 
	 * @see
	 */
	private class GetMusicListOfLastPlayed extends NdTask {

		/**
		 * Creates a new instance of GetMusicListOfLastPlayed. <br>
		 * Created 2014年9月11日 下午11:41:18
		 * 
         * @param context Context
         * @param resId int
		 */
		public GetMusicListOfLastPlayed(Context context, int resId) {
			super(context, resId);
		}

		@Override
		protected void doSuccess(Object result) {
			if (result != null) {
				lastPlayedList = (List<MusicInfo>) result;
				holder.txtPlayRecord.setText(getString(R.string.mbx_mymusic_music_info,
						lastPlayedList.size()));
			}
		}

		@Override
		protected void doFail(NdException result) {
			Toast.makeText(mContext, result.getSimpleMsg(), Toast.LENGTH_LONG).show();
		}

		@Override
		protected Object doInBackground(Object... arg0) {
			return ManagerFactory.getPlaylistInstance(getActivity()).getMusicListOfLastPlayed();
		}
	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		Intent intent = null;
		if(this.getActivity() == null){
			LogUtils.e("MyMusicFragment.java -- jiangwb", "onClick() -- fragment调用getActivity()得到null了");
			return;//by jiangwb bug: #61694
		}
		if (id == R.id.pnlIlike) {
			PlayerActivity.addSubActivity(this.getActivity());
			intent = new Intent(this.getActivity(), MyMusicLikeActivity.class);
			intent.putExtra(GlobalSetting.PARAMS_JSON, new Gson().toJson(favList));
			this.getActivity().startActivity(intent);

		} else if (id == R.id.pnlDownload) {
			PlayerActivity.addSubActivity(this.getActivity());
			intent = new Intent(this.getActivity(), MyMusicDownloadActivity.class);
			intent.putExtra(GlobalSetting.PARAMS_JSON, new Gson().toJson(downLoadList));
			this.getActivity().startActivity(intent);

		} else if (id == R.id.pnlPlayRecord) {
			PlayerActivity.addSubActivity(this.getActivity());
			intent = new Intent(this.getActivity(), MyMusicRecordActivity.class);
			intent.putExtra(GlobalSetting.PARAMS_JSON, new Gson().toJson(lastPlayedList));
			this.getActivity().startActivity(intent);

		} else if (id == R.id.btnRight) {
			getActivity().finish();

		}
	}
}
